package com.zhanghp.class07;

/**
 * <h1>这是错误的快排！不要看！</h1>
 *
 * @author zhanghp
 * @since 2024/2/3 17:57
 */
public class Code04_PartitionAndQuickSort_My_Wrong {
    public void quickSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        process(arr, 0, arr.length - 1);

    }

    public void process(int[] arr, int L, int R) {
        if (L == R) {
            return;
        }
        // 整个处理
        int[] partition = partition(arr, L, R);
        // 左处理
        process(arr, 0, partition[0]);
        // 右处理
        process(arr, partition[1], R);
    }


    public int[] partition(int[] arr, int L, int R) {
        int lIndex = L;
        int rIndex = R;
        int pointer = L;
        int fixVal = arr[R];

        while (pointer != R) {
            int curVal = arr[pointer];
            if (curVal < fixVal) {
                swap(arr, lIndex++, pointer++);
            } else if (curVal == fixVal) {
                pointer++;
            } else {
                swap(arr, rIndex--, pointer++);
            }
        }
        return new int[]{lIndex == L ? lIndex : --lIndex, rIndex};
    }


    public void swap(int[] arr, int a, int b) {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }



    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
        }
        return arr;
    }

    // for test
    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    // for test
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // for test
    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // for test
    public static void main(String[] args) {
//		int[] arr = { 7, 1, 3, 5, 4, 5, 1, 4, 2, 4, 2, 4 };
//
//		splitNum2(arr);
//		for (int i = 0; i < arr.length; i++) {
//			System.out.print(arr[i] + " ");
//		}

        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        Code04_PartitionAndQuickSort_My_Wrong my = new Code04_PartitionAndQuickSort_My_Wrong();
        Code04_PartitionAndQuickSort_Zuo_Answer zuo = new Code04_PartitionAndQuickSort_Zuo_Answer();

        System.out.println("test begin");
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            my.quickSort(arr1);
            zuo.equals(arr2);
            if (!isEqual(arr1, arr2)) {
                System.out.println("Oops!");
                succeed = false;
                break;
            }
        }
        System.out.println("test end");
        System.out.println(succeed ? "Nice!" : "Oops!");

    }
}
